<template>
  <div class="goods">
    <!--菜单-->
    <!--ref给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上-->
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li v-for="(item,index) in goods" :key="item.id" class="menu-item" :class="{'current':currentIndex===index}"
            @click="selectMenu(index,$event)">
          <span class="text border-1px">
            <support v-show="item.type>0" class="icon" :size="3" :type="item.type"></support>{{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <!--商品-->
    <div class="foods-wrapper" ref="foodWrapper">
      <ul>
        <li v-for="item in goods" class="food-list food-list-hook" :key="item.id">
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li v-for="food in item.foods" class="food-item border-1px" :key="food.id">
              <div class="icon">
                <img width="57" height="57" :src="food.icon">
              </div>
              <div class="content">
                <h2 class="name">{{food.name}}</h2>
                <p class="desc">{{food.description}}</p>
                <div class="extra">
                  <!-- <span>标签不分行避免换行产生的空白字符-->
                  <span class="count">月售{{food.sellCount}}份</span><span>好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="now">￥{{food.price}}</span>
                  <span class="old" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import support from '@/components/support/support'
  import BScroll from 'better-scroll'

  const ERROR_OK = 0
  export default {
    props: {
      seller: {
        type: Object
      }
    },
    data() {
      return {
        goods: [],
        listHeight: [], /* 存储每个类型区间的高度 */
        scrollY: 0 /* 存储菜单类型滚动的实时位置 */
      }
    },
    computed: {
      // 计算商品滑动到某个区间
      currentIndex() {
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i]
          let height2 = this.listHeight[i + 1]

          // !height2 表示滑动到最后一个区间的时候返回上一个区间的索引
          if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            return i
          }
        }
        return 0
      }
    },
    created() {
      this.getGoods()
    },
    methods: {
      getGoods() {
        this.$axios.get('/api/goods').then(response => {
          response = response.data
          if (response.errno === ERROR_OK) {
            this.goods = response.data
            // 在下次 DOM 更新循环结束之后执行延迟回调
            // 执行DOM操作使用nextTick
            this.$nextTick(() => {
              this._initScroll()
              this._calculateHeight()
            })
          }
        })
      },
      // 初始化滚动、监听滚动Y 轴的位置
      _initScroll() {
        // BScroll(DOM对象 ,JSON数据)
        this.menuScroll = new BScroll(this.$refs.menuWrapper, {
          // better-scroll 默认会阻止浏览器的原生 click 事件
          click: true
        })

        this.foodScroll = new BScroll(this.$refs.foodWrapper, {
          // 获取实时滚动的位置
          probeType: 3,
          click: true
        })

        // 监听滚动位置
        this.foodScroll.on('scroll', (post) => {
          // 获取实时Y 轴坐标
          this.scrollY = Math.abs(Math.round(post.y))
        })
      },
      // 计算每个类型区间的高度
      _calculateHeight() {
        // 通过每个DOM对象获取高度
        // 获取所有类型区间的对象
        let foodList = this.$refs.foodWrapper.getElementsByClassName('food-list-hook')
        let height = 0
        // 保存第一个区间显示的高度
        this.listHeight.push(height)
        for (let i = 0; i < foodList.length; i++) {
          // 获取每个区间的开始高度
          height += foodList[i].clientHeight
          /* 元素客户区的大小 */

          // 保存每个区间的开始高度
          this.listHeight.push(height)
        }
      },
      selectMenu(index, event) {
        // better-scroll 派发的 event 参数加一个私有属性 _constructed
        //  _constructed是 better-scroll 私有的，区别原生的点击事件
        // 如果是浏览器的点击就不做处理否则就滚动到相应位置
        if (!event._constructed) {
          return
        }
        // 获取DOM对象
        let foodList = this.$refs.foodWrapper.getElementsByClassName('food-list-hook')

        // 根据索引获取具体位置的DOM对象
        let el = foodList[index]

        // 在300ms内滚动到el的DOM对象位置
        this.foodScroll.scrollToElement(el, 300)
      }
    },
    components: {
      support
    }
  }
</script>

<style lang="less" rel="stylesheet/less">
  @import "./../../common/less/mixins";

  .goods {
    display: flex;
    position: absolute;
    top: 174px;
    bottom: 46px;
    width: 100%;
    overflow: hidden;
    .menu-wrapper {
      flex: 0 0 80px;
      width: 80px;
      background: #f3f5f7;
      .menu-item {
        display: table; /*垂直居中*/
        height: 54px;
        width: 56px;
        line-height: 14px;
        padding: 0 12px;
        .icon {
          display: inline-block;
          vertical-align: top;
          width: 12px;
          height: 12px;
          margin-right: 2px;
          background-size: 12px 12px;
          background-repeat: no-repeat;
        }
        .text {
          display: table-cell; /*此元素会作为一个表格单元格显示*/
          width: 56px;
          vertical-align: middle; /*居中*/
          font-size: 12px;
          .border-1px(rgba(7, 17, 27, 0.1));
        }
        /*滚动位置设置高亮*/
        &.current {
          position: relative;
          z-index: 10;
          margin-top: -1px;
          background: #fff;
          font-weight: 700;
          .text {
            .border-none()
          }
        }
      }
    }
    .foods-wrapper {
      flex: 1;
      .title {
        padding-left: 14px;
        height: 26px;
        line-height: 26px;
        border-left: 2px solid #d9dde1;
        font-size: 12px;
        color: rgb(147, 153, 159);
        background: #f3f5f7;
      }
      .food-item {
        display: flex;
        margin: 18px;
        padding-bottom: 18px;
        .border-1px(rgba(7, 17, 27, .1));
        &:last-child {
          .border-none;
          margin-bottom: 0;
        }
        .icon {
          flex: 0 0 57px;
          margin-right: 10px;
        }
        .content {
          flex: 1;
          .name {
            margin: 2px 0 8px 0;
            height: 14px;
            line-height: 14px;
            font-size: 14px;
            color: rgb(7, 17, 27);
          }
          .desc, .extra {
            line-height: 10px;
            color: rgb(147, 153, 159);
            font-size: 10px;
          }
          .desc {
            margin-bottom: 8px;
            line-height: 12px;
          }
          .extra {
            .count {
              margin-right: 12px;
            }
          }
          .price {
            font-weight: 700;
            line-height: 24px;
            .now {
              margin-right: 8px;
              font-size: 14px;
              color: rgb(240, 20, 20);
            }
            .old {
              text-decoration: line-through; // 定义穿过文本下的一条线
              font-size: 10px;
              color: rgb(147, 153, 159);
            }
          }
        }
      }
    }
  }
</style>
